import ttkbootstrap as ttk
import datetime
from ttkbootstrap.constants import *
from process_manager import ProcessManager
from event_manager import EventManager
from file_manager import FileManager

import os
import shutil

class SplashScreen:
    def __init__(self, root):
        self.root = root
        self.splash = ttk.Toplevel(root)
        self.splash.overrideredirect(True)
        self.splash.geometry("1024x768")
        self.splash.configure(bg='#1a1a2e')
        
        # 居中显示
        screen_width = root.winfo_screenwidth()
        screen_height = root.winfo_screenheight()
        x = (screen_width - 1024) // 2
        y = (screen_height - 768) // 2
        self.splash.geometry(f"1024x768+{x}+{y}")
        
        # 系统Logo
        self.logo_label = ttk.Label(
            self.splash, 
            text='MASX OS', 
            font=('Segoe UI', 48),
            foreground='#4a6cf7',
            background='#1a1a2e'
        )
        self.logo_label.pack(pady=(200, 50))
        
        # 加载进度条
        self.progress_var = ttk.DoubleVar()
        self.progress_bar = ttk.Progressbar(
            self.splash, 
            variable=self.progress_var, 
            length=500, 
            mode='determinate',
            bootstyle='success'
        )
        self.progress_bar.pack(pady=20)
        
        # 加载文本
        self.status_label = ttk.Label(
            self.splash, 
            text='正在加载系统...', 
            font=('Segoe UI', 14),
            foreground='#e0e0ff',
            background='#1a1a2e'
        )
        self.status_label.pack(pady=10)
        
        # 模拟加载过程
        self.load_progress()
        
    def load_progress(self):
        current = self.progress_var.get()
        if current < 100:
            # 模拟不同阶段的加载
            if current < 20:
                self.status_label.config(text='初始化内核...')
            elif current < 50:
                self.status_label.config(text='加载系统服务...')
            elif current < 80:
                self.status_label.config(text='初始化用户界面...')
            else:
                self.status_label.config(text='准备就绪...')
            
            self.progress_var.set(current + 0.5)
            self.splash.after(30, self.load_progress)
        else:
            # 加载完成，关闭启动界面
            self.splash.destroy()
            # 启动主系统
            self.os = MASXOS(self.root)

class MASXOS:
    def __init__(self, root):
        self.root = root
        self.root.title("MASX OS")
        self.root.geometry("1024x768")
        self.root.configure(bg='#f5f5f7')
        
        self.style = ttk.Style()
        # 配置Apple风格主题
        # 现代化深色主题
        self.style.configure('.',
                            background='#1a1a2e',
                            foreground='#e0e0ff',
                            font=('Segoe UI', 10))
        self.style.configure('TButton',
                            background='#4a6cf7',
                            foreground='white',
                            borderwidth=0,
                            borderradius=12,
                            padding=8,
                            font=('Segoe UI', 10, 'bold'))
        # macOS窗口控制按钮样式
        self.style.configure('MacClose.TButton',
                            background='#FF5F56',
                            borderwidth=0,
                            borderradius=10,
                            width=10,
                            height=10)
        self.style.configure('MacMin.TButton',
                            background='#FFCC00',
                            borderwidth=0,
                            borderradius=10,
                            width=10,
                            height=10)
        # 按钮点击和悬停动画效果
        self.style.map('TButton',
                      background=[('active', '#3a5ce7'), 
                                 ('pressed', '#2d47b9'),
                                 ('disabled', '#333855'),
                                 ('hover', '#5b7af8')],
                      relief=[('pressed', FLAT), ('!pressed', FLAT)])
        self.style.configure('TFrame',
                            background='#1a1a2e',
                            borderwidth=0,
                            borderradius=12)
        # 滚动条样式
        self.style.configure('TScrollbar',
                            background='#2d2d44',
                            troughcolor='#1a1a2e',
                            arrowcolor='#e0e0ff',
                            borderwidth=0)
        # 地址栏样式
        self.style.configure('Address.TEntry',
                            fieldbackground='#2d2d44',
                            foreground='#e0e0ff',
                            borderwidth=0,
                            borderradius=8,
                            padding=6)
        # iOS风格图标框架样式
        self.style.configure('IconFrame.TFrame',
                            background='transparent',
                            borderwidth=0,
                            borderradius=16)
        self.style.configure('TLabel',
                            background='#f5f5f7',
                            foreground='#333333')
        
        # 延迟初始化未使用的管理器，减少启动资源占用
        # self.process_manager = ProcessManager()
        # self.event_manager = EventManager()
        self.start_menu_visible = False
        self.taskbar_buttons = {}  # 初始化任务栏按钮字典
        
        # 初始化微内核
        from microkernel import MicroKernel
        self.kernel = MicroKernel()
        self.kernel.start()
        
        # 注册核心服务
        self.register_core_services()
        
        self.init_ui()
        self.root.deiconify()  # 显示主窗口
        self.check_kernel_messages()

if __name__ == "__main__":
    try:
        # 创建主窗口但先隐藏
        root = ttk.Window(themename='darkly')
        root.withdraw()  # 隐藏主窗口
        # 显示启动界面
        splash = SplashScreen(root)
        root.mainloop()
    except Exception as e:
        print(f"MASX OS启动失败: {str(e)}")
        with open("error.log", "w") as f:
            f.write(f"{str(e)}")